home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / rpc_pipes / pipesamr.c.z / pipesamr.c
Encoding:
C/C++ Source or Header  |  1998-10-28  |  13.5 KB  |  498 lines

  1.  
  2. /* 
  3.  *  Unix SMB/Netbios implementation.
  4.  *  Version 1.9.
  5.  *  RPC Pipe client / server routines
  6.  *  Copyright (C) Andrew Tridgell              1992-1997,
  7.  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
  8.  *  Copyright (C) Paul Ashton                       1997.
  9.  *  
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *  
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *  
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25.  
  26. #include "../includes.h"
  27. #include "../trans2.h"
  28. #include "../nterr.h"
  29.  
  30. extern int DEBUGLEVEL;
  31.  
  32. #ifdef NTDOMAIN
  33.  
  34. /*******************************************************************
  35.  samr_reply_unknown_1
  36.  ********************************************************************/
  37. static int samr_reply_close(SAMR_Q_CLOSE *q_u,
  38.                 char *q, char *base)
  39. {
  40.     SAMR_R_CLOSE r_u;
  41.  
  42.     /* set up the SAMR unknown_1 response */
  43.     bzero(&(r_u.pol.data), POL_HND_SIZE);
  44.  
  45.     /* close the policy handle */
  46.     if (close_lsa_policy_hnd(&(q_u->pol)))
  47.     {
  48.         r_u.status = 0;
  49.     }
  50.     else
  51.     {
  52.         r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_INVALID;
  53.     }
  54.  
  55.     DEBUG(5,("samr_unknown_1: %d\n", __LINE__));
  56.  
  57.     /* store the response in the SMB stream */
  58.     q = samr_io_r_close(False, &r_u, q, base, 4, 0);
  59.  
  60.     DEBUG(5,("samr_unknown_1: %d\n", __LINE__));
  61.  
  62.     /* return length of SMB data stored */
  63.     return PTR_DIFF(q, base);
  64. }
  65.  
  66. /*******************************************************************
  67.  api_samr_close
  68.  ********************************************************************/
  69. static void api_samr_close( char *param, char *data,
  70.                                     char **rdata, int *rdata_len )
  71. {
  72.     SAMR_Q_CLOSE q_u;
  73.  
  74.     /* grab the samr unknown 1 */
  75.     samr_io_q_close(True, &q_u, data + 0x18, data, 4, 0);
  76.  
  77.     /* construct reply.  always indicate success */
  78.     *rdata_len = samr_reply_close(&q_u, *rdata + 0x18, *rdata);
  79. }
  80.  
  81.  
  82. /*******************************************************************
  83.  samr_reply_open_secret
  84.  ********************************************************************/
  85. static int samr_reply_open_secret(SAMR_Q_OPEN_SECRET *q_u,
  86.                 char *q, char *base)
  87. {
  88.     SAMR_R_OPEN_SECRET r_u;
  89.  
  90.     r_u.status = 0x0;
  91.     /* get a (unique) handle.  open a policy on it. */
  92.     if (r_u.status == 0x0 && !open_lsa_policy_hnd(&(r_u.pol)))
  93.     {
  94.         r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
  95.     }
  96.  
  97.     /* associate the domain SID with the (unique) handle. */
  98.     if (r_u.status == 0x0 && !set_lsa_policy_samr_sid(&(r_u.pol), &(q_u->dom_sid)))
  99.     {
  100.         /* oh, whoops.  don't know what error message to return, here */
  101.         r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
  102.     }
  103.  
  104.     DEBUG(5,("samr_open_secret: %d\n", __LINE__));
  105. /*
  106.     make_samr_r_open_secret(&r_u, q_u->open_policy, my_name, domain, status);
  107. */
  108.     /* store the response in the SMB stream */
  109.     q = samr_io_r_open_secret(False, &r_u, q, base, 4, 0);
  110.  
  111.     DEBUG(5,("samr_open_secret: %d\n", __LINE__));
  112.  
  113.     /* return length of SMB data stored */
  114.     return PTR_DIFF(q, base);
  115. }
  116.  
  117. /*******************************************************************
  118.  api_samr_open_secret
  119.  ********************************************************************/
  120. static void api_samr_open_secret( char *param, char *data,
  121.                                     char **rdata, int *rdata_len )
  122. {
  123.     SAMR_Q_OPEN_SECRET q_u;
  124.  
  125.     /* grab the samr open */
  126.     samr_io_q_open_secret(True, &q_u, data + 0x18, data, 4, 0);
  127.  
  128.     /* construct reply.  always indicate success */
  129.     *rdata_len = samr_reply_open_secret(&q_u, *rdata + 0x18, *rdata);
  130. }
  131.  
  132.  
  133. /*******************************************************************
  134.  samr_reply_lookup_rids
  135.  ********************************************************************/
  136. static int samr_reply_lookup_rids(SAMR_Q_LOOKUP_RIDS *q_u,
  137.                 char *q, char *base)
  138. {
  139.     struct smb_passwd *smb_pass;
  140.     fstring mach_acct;
  141.  
  142.     uint32 lookup_rid = 0;
  143.     uint32 status     = 0;
  144.  
  145.     SAMR_R_LOOKUP_RIDS r_u;
  146.  
  147.     DEBUG(5,("samr_lookup_rids: %d\n", __LINE__));
  148.  
  149.     fstrcpy(mach_acct, unistrn2(q_u->uni_mach_acct.buffer,
  150.                                 q_u->uni_mach_acct.uni_str_len));
  151.  
  152.     /* lkclXXXX SHOULD use name_to_rid() here! */
  153.     {
  154.         /* find the machine account */
  155.         become_root(True);
  156.         smb_pass = get_smbpwd_entry(mach_acct, 0);
  157.         unbecome_root(True);
  158.  
  159.         if (smb_pass == NULL)
  160.         {
  161.             /* machine account doesn't exist: say so */
  162.             status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
  163.             lookup_rid = 0;
  164.         }
  165.         else
  166.         {
  167.             status = 0;
  168.             lookup_rid = smb_pass->smb_userid;
  169.         }
  170.     }
  171.  
  172.     make_samr_r_lookup_rids(&r_u, 1, lookup_rid, status);
  173.  
  174.     /* store the response in the SMB stream */
  175.     q = samr_io_r_lookup_rids(False, &r_u, q, base, 4, 0);
  176.  
  177.     DEBUG(5,("samr_lookup_rids: %d\n", __LINE__));
  178.  
  179.     /* return length of SMB data stored */
  180.     return PTR_DIFF(q, base);
  181. }
  182.  
  183. /*******************************************************************
  184.  api_samr_lookup_rids
  185.  ********************************************************************/
  186. static void api_samr_lookup_rids( char *param, char *data,
  187.                                     char **rdata, int *rdata_len )
  188. {
  189.     SAMR_Q_LOOKUP_RIDS q_u;
  190.  
  191.     /* grab the samr unknown 11 */
  192.     samr_io_q_lookup_rids(True, &q_u, data + 0x18, data, 4, 0);
  193.  
  194.     /* construct reply.  always indicate success */
  195.     *rdata_len = samr_reply_lookup_rids(&q_u, *rdata + 0x18, *rdata);
  196. }
  197.  
  198.  
  199. /*******************************************************************
  200.  samr_reply_unknown_22
  201.  ********************************************************************/
  202. static int samr_reply_unknown_22(SAMR_Q_UNKNOWN_22 *q_u,
  203.                 char *q, char *base,
  204.                 int status)
  205. {
  206.     SAMR_R_UNKNOWN_22 r_u;
  207.  
  208.     /* set up the SAMR unknown_22 response */
  209.     bzero(&(r_u.pol.data), POL_HND_SIZE);
  210.  
  211.     r_u.status = 0x0;
  212.  
  213.     /* get a (unique) handle.  open a policy on it. */
  214.     if (r_u.status == 0x0 && !open_lsa_policy_hnd(&(r_u.pol)))
  215.     {
  216.         r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
  217.     }
  218.  
  219.     DEBUG(5,("samr_unknown_22: %d\n", __LINE__));
  220.  
  221.     /* store the response in the SMB stream */
  222.     q = samr_io_r_unknown_22(False, &r_u, q, base, 4, 0);
  223.  
  224.     DEBUG(5,("samr_unknown_22: %d\n", __LINE__));
  225.  
  226.     /* return length of SMB data stored */
  227.     return PTR_DIFF(q, base);
  228. }
  229.  
  230. /*******************************************************************
  231.  api_samr_unknown_22
  232.  ********************************************************************/
  233. static void api_samr_unknown_22( char *param, char *data,
  234.                                     char **rdata, int *rdata_len )
  235. {
  236.     SAMR_Q_UNKNOWN_22 q_u;
  237.  
  238.     /* grab the samr unknown 22 */
  239.     samr_io_q_unknown_22(True, &q_u, data + 0x18, data, 4, 0);
  240.  
  241.     /* construct reply.  always indicate success */
  242.     *rdata_len = samr_reply_unknown_22(&q_u, *rdata + 0x18, *rdata, 0x0);
  243. }
  244.  
  245.  
  246. /*******************************************************************
  247.  samr_reply_unknown_24
  248.  ********************************************************************/
  249. static int samr_reply_unknown_24(SAMR_Q_UNKNOWN_24 *q_u,
  250.                 char *q, char *base,
  251.                 int status)
  252. {
  253.     SAMR_R_UNKNOWN_24 r_u;
  254.     NTTIME expire;
  255.  
  256.     DEBUG(5,("samr_unknown_24: %d\n", __LINE__));
  257.  
  258.     expire.low  = 0xffffffff;
  259.     expire.high = 0x7fffffff;
  260.  
  261.     make_samr_r_unknown_24(&r_u, q_u->unknown_0,
  262.                 &expire, "BROOKFIELDS$", 0x03ef, 0x0);
  263.  
  264.     /* store the response in the SMB stream */
  265.     q = samr_io_r_unknown_24(False, &r_u, q, base, 4, 0);
  266.  
  267.     DEBUG(5,("samr_unknown_24: %d\n", __LINE__));
  268.  
  269.     /* return length of SMB data stored */
  270.     return PTR_DIFF(q, base);
  271. }
  272.  
  273. /*******************************************************************
  274.  api_samr_unknown_24
  275.  ********************************************************************/
  276. static void api_samr_unknown_24( char *param, char *data,
  277.                                     char **rdata, int *rdata_len )
  278. {
  279.     SAMR_Q_UNKNOWN_24 q_u;
  280.  
  281.     /* grab the samr unknown 24 */
  282.     samr_io_q_unknown_24(True, &q_u, data + 0x18, data, 4, 0);
  283.  
  284.     /* construct reply.  always indicate success */
  285.     *rdata_len = samr_reply_unknown_24(&q_u, *rdata + 0x18, *rdata, 0x0);
  286. }
  287.  
  288.  
  289. /*******************************************************************
  290.  samr_reply_unknown_32
  291.  ********************************************************************/
  292. static int samr_reply_unknown_32(SAMR_Q_UNKNOWN_32 *q_u,
  293.                 char *q, char *base,
  294.                 int status)
  295. {
  296.     int i;
  297.     SAMR_R_UNKNOWN_32 r_u;
  298.  
  299.     /* set up the SAMR unknown_32 response */
  300.     bzero(&(r_u.pol.data), POL_HND_SIZE);
  301.     if (status == 0)
  302.     {
  303.         for (i = 4; i < POL_HND_SIZE; i++)
  304.         {
  305.             r_u.pol.data[i] = i+1;
  306.         }
  307.     }
  308.  
  309.     r_u.unknown_0 = 0x00000030;
  310.     r_u.padding   = 0x0;
  311.     r_u.status = status;
  312.  
  313.     DEBUG(5,("samr_unknown_32: %d\n", __LINE__));
  314. /*
  315.     make_samr_r_unknown_32(&r_u, q_u->unknown_32, my_name, domain, status);
  316. */
  317.     /* store the response in the SMB stream */
  318.     q = samr_io_r_unknown_32(False, &r_u, q, base, 4, 0);
  319.  
  320.     DEBUG(5,("samr_unknown_32: %d\n", __LINE__));
  321.  
  322.     /* return length of SMB data stored */
  323.     return PTR_DIFF(q, base);
  324. }
  325.  
  326. /*******************************************************************
  327.  api_samr_unknown_32
  328.  ********************************************************************/
  329. static void api_samr_unknown_32( char *param, char *data,
  330.                                     char **rdata, int *rdata_len )
  331. {
  332.     uint32 status = 0;
  333.     struct smb_passwd *smb_pass;
  334.     fstring mach_acct;
  335.  
  336.     SAMR_Q_UNKNOWN_32 q_u;
  337.  
  338.     /* grab the samr unknown 32 */
  339.     samr_io_q_unknown_32(True, &q_u, data + 0x18, data, 4, 0);
  340.  
  341.     /* find the machine account: tell the caller if it exists.
  342.        lkclXXXX i have *no* idea if this is a problem or not
  343.        or even if you are supposed to construct a different
  344.        reply if the account already exists...
  345.      */
  346.  
  347.     fstrcpy(mach_acct, unistrn2(q_u.uni_mach_acct.buffer,
  348.                                 q_u.uni_mach_acct.uni_str_len));
  349.  
  350.     become_root(True);
  351.     smb_pass = get_smbpwd_entry(mach_acct, 0);
  352.     unbecome_root(True);
  353.  
  354.     if (smb_pass != NULL)
  355.     {
  356.         /* machine account exists: say so */
  357.         status = 0xC0000000 | NT_STATUS_USER_EXISTS;
  358.     }
  359.     else
  360.     {
  361.         /* this could cause trouble... */
  362.         status = 0;
  363.     }
  364.  
  365.     /* construct reply. */
  366.     *rdata_len = samr_reply_unknown_32(&q_u, *rdata + 0x18, *rdata, status);
  367. }
  368.  
  369.  
  370. /*******************************************************************
  371.  samr_reply_open_policy
  372.  ********************************************************************/
  373. static int samr_reply_open_policy(SAMR_Q_OPEN_POLICY *q_u,
  374.                 char *q, char *base)
  375. {
  376.     SAMR_R_OPEN_POLICY r_u;
  377.  
  378.     /* set up the SAMR open_policy response */
  379.  
  380.     r_u.status = 0x0;
  381.     /* get a (unique) handle.  open a policy on it. */
  382.     if (r_u.status == 0x0 && !open_lsa_policy_hnd(&(r_u.pol)))
  383.     {
  384.         r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
  385.     }
  386.  
  387.     /* associate the domain SID with the (unique) handle. */
  388.     if (r_u.status == 0x0 && !set_lsa_policy_samr_pol_status(&(r_u.pol), q_u->unknown_0))
  389.     {
  390.         /* oh, whoops.  don't know what error message to return, here */
  391.         r_u.status = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
  392.     }
  393.  
  394.     DEBUG(5,("samr_open_policy: %d\n", __LINE__));
  395. /*
  396.     make_samr_r_open_policy(&r_u, q_u->unknown_0, my_name, domain, status);
  397. */
  398.     /* store the response in the SMB stream */
  399.     q = samr_io_r_open_policy(False, &r_u, q, base, 4, 0);
  400.  
  401.     DEBUG(5,("samr_open_policy: %d\n", __LINE__));
  402.  
  403.     /* return length of SMB data stored */
  404.     return PTR_DIFF(q, base);
  405. }
  406.  
  407. /*******************************************************************
  408.  api_samr_open_policy
  409.  ********************************************************************/
  410. static void api_samr_open_policy( char *param, char *data,
  411.                                     char **rdata, int *rdata_len )
  412. {
  413.     SAMR_Q_OPEN_POLICY q_u;
  414.  
  415.     /* grab the samr open policy */
  416.     samr_io_q_open_policy(True, &q_u, data + 0x18, data, 4, 0);
  417.  
  418.     /* construct reply.  always indicate success */
  419.     *rdata_len = samr_reply_open_policy(&q_u, *rdata + 0x18, *rdata);
  420. }
  421.  
  422.  
  423. /*******************************************************************
  424.  receives a samr pipe and responds.
  425.  ********************************************************************/
  426. BOOL api_samrTNP(int cnum,int uid, char *param,char *data,
  427.              int mdrcnt,int mprcnt,
  428.              char **rdata,char **rparam,
  429.              int *rdata_len,int *rparam_len)
  430. {
  431.     RPC_HDR_RR hdr;
  432.  
  433.     if (data == NULL)
  434.     {
  435.         DEBUG(2,("api_samrTNP: NULL data received\n"));
  436.         return False;
  437.     }
  438.  
  439.     smb_io_rpc_hdr_rr(True, &hdr, data, data, 4, 0);
  440.  
  441.     DEBUG(4,("samr TransactNamedPipe op %x\n",hdr.opnum));
  442.  
  443.     switch (hdr.opnum)
  444.     {
  445.         case SAMR_CLOSE:
  446.         {
  447.             api_samr_close( param, data, rdata, rdata_len);
  448.             create_rpc_reply(hdr.hdr.call_id, *rdata, *rdata_len);
  449.             break;
  450.         }
  451.         case SAMR_OPEN_SECRET:
  452.         {
  453.             api_samr_open_secret( param, data, rdata, rdata_len);
  454.             create_rpc_reply(hdr.hdr.call_id, *rdata, *rdata_len);
  455.             break;
  456.         }
  457.         case SAMR_LOOKUP_RIDS:
  458.         {
  459.             api_samr_lookup_rids( param, data, rdata, rdata_len);
  460.             create_rpc_reply(hdr.hdr.call_id, *rdata, *rdata_len);
  461.             break;
  462.         }
  463.         case 0x22:
  464.         {
  465.             api_samr_unknown_22( param, data, rdata, rdata_len);
  466.             create_rpc_reply(hdr.hdr.call_id, *rdata, *rdata_len);
  467.             break;
  468.         }
  469.         case 0x24:
  470.         {
  471.             api_samr_unknown_24( param, data, rdata, rdata_len);
  472.             create_rpc_reply(hdr.hdr.call_id, *rdata, *rdata_len);
  473.             break;
  474.         }
  475.         case 0x32:
  476.         {
  477.             api_samr_unknown_32( param, data, rdata, rdata_len);
  478.             create_rpc_reply(hdr.hdr.call_id, *rdata, *rdata_len);
  479.             break;
  480.         }
  481.         case SAMR_OPEN_POLICY:
  482.         {
  483.             api_samr_open_policy( param, data, rdata, rdata_len);
  484.             create_rpc_reply(hdr.hdr.call_id, *rdata, *rdata_len);
  485.             break;
  486.         }
  487.         default:
  488.         {
  489.             DEBUG(4, ("samr, unknown code: %lx\n", hdr.opnum));
  490.             break;
  491.         }
  492.     }
  493.  
  494.     return(True);
  495. }
  496.  
  497. #endif
  498.